package fleet

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestDiscoverManifests(t *testing.T) {
	testdata := []struct {
		name              string
		rootDir           string
		expectedPipelines []string
	}{
		{
			name:    "Scenario 1",
			rootDir: "testdata",
			expectedPipelines: []string{`name: 'deps(rancher/fleet): bump "grafana" Fleet bundle for "grafana" Helm chart'
sources:
  grafana:
    name: 'Get latest "grafana" Helm chart version'
    kind: 'helmchart'
    spec:
      name: 'grafana'
      url: 'https://grafana.github.io/helm-charts'
      versionfilter:
        kind: 'semver'
        pattern: '*'
conditions:
  grafana-name:
    name: 'Ensure Helm chart name grafana is specified'
    kind: 'yaml'
    disablesourceinput: true
    spec:
      file: 'fleet.d/grafana/fleet.yaml'
      key: '$.helm.chart'
      value: 'grafana'
  grafana-repository:
    name: 'Ensure Helm chart repository https://grafana.github.io/helm-charts is specified'
    kind: 'yaml'
    disablesourceinput: true
    spec:
      file: 'fleet.d/grafana/fleet.yaml'
      key: '$.helm.repo'
      value: 'https://grafana.github.io/helm-charts'
targets:
  grafana:
    name: 'deps(helm): bump chart "grafana" in Fleet bundle "grafana"'
    kind: 'yaml'
    spec:
      file: 'fleet.d/grafana/fleet.yaml'
      key: '$.helm.version'
    sourceid: 'grafana'
`},
		},
	}

	for _, tt := range testdata {

		t.Run(tt.name, func(t *testing.T) {
			fleet, err := New(
				Spec{}, tt.rootDir, "", "")

			require.NoError(t, err)

			var pipelines []string
			rawPipelines, err := fleet.DiscoverManifests()
			require.NoError(t, err)

			for i := range rawPipelines {
				// We expect manifest generated by the autodiscovery to use the yaml syntax
				pipelines = append(pipelines, string(rawPipelines[i]))
				assert.Equal(t, tt.expectedPipelines[i], pipelines[i])
			}
		})
	}

}
